package sddz.foodmanage.dialog;

import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

import sddz.foodmanage.R;
import sddz.foodmanage.adapter.UniversalAdapter;
import sddz.foodmanage.adapter.UniversalViewHolder;
import sddz.foodmanage.base.BaseWindow;
import sddz.foodmanage.model.ClassifyBean;
import sddz.foodmanage.model.PingjiBean;

/**
 * @Date:2019/7/9
 * @Author:lbb
 * 评级
 */

public class PingjiWindow extends BaseWindow {
    private final PopupWindow popupWindow;
    private List<PingjiBean> mList = new ArrayList<>();
    private UniversalAdapter<PingjiBean> adapter;
    private final ListView listView;

    public PingjiWindow(AppCompatActivity mActivity) {
        super(mActivity);

        View view = LayoutInflater.from(mActivity).inflate(R.layout.sort_layout, null);
        listView = (ListView) view.findViewById(R.id.sort_list);

        popupWindow = new PopupWindow(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setAnimationStyle(R.style.WindowAnim);
        initListener();
    }

    private void initListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                if (onpjItemListener != null) {
                    onpjItemListener.nation(mList.get(position).getPjname(), mList.get(position).getPjid()+"");
                }
            }
        });

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1f);
            }
        });
    }

    public void setData(List<PingjiBean> mList) {
        this.mList = mList;
        adapter = new UniversalAdapter<PingjiBean>(mActivity, mList, R.layout.text_simple_item_layout) {
            @Override
            public void convert(UniversalViewHolder holder, int position, PingjiBean nationBean) {
                holder.setText(R.id.text_simple, nationBean.getPjname());
            }
        };
        listView.setAdapter(adapter);
    }

    public void showWindow(View view) {
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        setAlpha(0.6f);
    }

    public void dismiss() {
        popupWindow.dismiss();
    }

    private OnpjItemListener onpjItemListener;

    public void setOnNationItemListener(PingjiWindow.OnpjItemListener onpjItemListener) {
        this.onpjItemListener = onpjItemListener;
    }

    public interface OnpjItemListener {
        void nation(String nation, String nationCode);
    }
}
